﻿using CongregacaoOs.Business;
using CongregacaoOs.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CongregacaoOs.WebForm.Cadastros.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Session.Clear();
        }

        protected void btnAutenticar_Click(object sender, EventArgs e)
        {
            if (Membership.ValidateUser(txtUsuario.Text.Trim(), txtSenha.Text.Trim()))
            {
                string[] roles;

                try
                {
                    roles = Roles.GetRolesForUser(txtUsuario.Text.Trim());
                    if (roles.ToArray().Count() > 0)
                    {
                        Session["TipoUsuario"] = roles[0].ToString();
                        MembershipUser user = Membership.GetUser(txtUsuario.Text.Trim());

                        GetServantFeatures1_Result dadosGerais = new GetServantFeatures1_Result();
                        ccPessoaBus bus = new ccPessoaBus();

                        dadosGerais = bus.PessoaFeaturesServantOfGod((Guid)(user.ProviderUserKey));

                        Session["DadosGerais"] = dadosGerais;
                    }

                    Session["CurrentUser"] = txtUsuario.Text.Trim();
                    Response.Redirect("~/cadastros/home.aspx");
                }
                catch (Exception ea)
                {
                    MessageWindow.MessageType = false;
                    MessageWindow.Texto = "Erro ao logar, tente novamente. Caso persista contate o administrador do sistema!";
                    MessageWindow.ShowWindow();
                }
            }
            else
            {
                MessageWindow.MessageType = false;
                MessageWindow.Texto = "Login ou senha inválidos, tente novamente!";
                MessageWindow.ShowWindow();
            }
        }
    }
}